/* ************************************************************************

   Copyright:

   License:

   Authors:

************************************************************************ */

/* ************************************************************************

#asset(qcodereview/*)

************************************************************************ */

/**
 * This is the main application class of your custom application "qCodeReview"
 */
qx.Class.define("qcodereview.Application",
{
  extend : qx.application.Standalone,



  /*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */

  members :
  {
    /**
     * This method contains the initial application code and gets called 
     * during startup of the application
     * 
     * @lint ignoreDeprecated(alert)
     */
    __rpc : new qx.io.remote.Rpc("http://localhost:8080/s/", "codeReviewService"),
    __tree : null,
    main : function()
    {
      // Call super class
      this.base(arguments);

      // Enable logging in debug variant
//      if (qx.core.Environment.get("qx.debug"))
//      {
        // support native logging capabilities, e.g. Firebug for Firefox
        qx.log.appender.Native;
        // support additional cross-browser console. Press F7 to toggle visibility
        qx.log.appender.Console;
//      }

      /*
      -------------------------------------------------------------------------
        Below is your actual application code...
      -------------------------------------------------------------------------
      */


      // Document is the application root
      var doc = this.getRoot();
      var dock = new qx.ui.layout.Dock(3, 10);
      dock.setSort("y");
      
      var widget = new qx.ui.container.Composite(dock).set(
      {
        decorator: "main",
//        backgroundColor: "yellow",
        allowGrowX : true,
        allowGrowY : true
      });
      doc.add(widget, {edge:0});

      this.createTree();
      this.createCodeView();
      widget.add(this.__tree, {edge:'west'});
      widget.add(this.__codeView, {edge:'east', flex:1});
      var that = this;
      this.__tree.addListener("changeSelection", function(e){
            //this.debug('changeSelection');
            var selection = e.getData();
            for(var i = 0; i < selection.length; i++) {
                if( selection[i] != this.__tree.getRoot() ) {
                    this.debug('selected:'+selection[i].getLabel());
                    this.__rpc.callAsync(function(fileContent) {
                        that.__codeView.setHtml(fileContent);
                        widget.remove(that.__tree);
                    }, "show", selection[i].getLabel());

                }
            }
          }, this);
      //doc.add(, {edge:"west"});

      //doc.add(button1, {left: 100, top: 50});
      //doc.add(tree, {left: 0, top: 0});
    },

    createCodeView : function() {
        var view = new qx.ui.embed.Html("<div class='script'>The sample JS source will be displayed here.</div>");
        view.setOverflow("auto", "auto");
//        view.setFont("monospace");
//        view.setBackgroundColor("white");
        this.__codeView = view
    },

    createTree : function(){
        // Create the tree widget
        this.__tree = new qx.ui.tree.Tree().set({'minWidth':250});
        var root = new qx.ui.tree.TreeFolder("root");
        this.__tree.setRoot(root);
        var that = this;
        var handler = function(fileData) {
            that.setTree(fileData);
        };
        this.__rpc.callAsync(handler, "list");

//        fileData = this.__rpc.callSync("list");
//        this.setTree(fileData);
        // Create a folder and set it the root folder
        // Create some folders:
//        var te1_1 = new qx.ui.tree.TreeFolder("Files");
//        var te1_2 = new qx.ui.tree.TreeFolder("Workspace");
//        var te1_3 = new qx.ui.tree.TreeFolder("Network");
//        var te1_4 = new qx.ui.tree.TreeFolder("Trash");

        // Create some content (leaves) and add it to the "Files" folder:
//        var te1_2_1 = new qx.ui.tree.TreeFile("Windows (C:)");
//        var te1_2_2 = new qx.ui.tree.TreeFile("Documents (D:)");
//        te1_2.add(te1_2_1, te1_2_2);

        // Add the content to the root folder
//        root.add(te1_1, te1_2, te1_3, te1_4);
    },

    _setTree : function(tree, fileData) {
        var root = tree.getRoot();
        if( typeof fileData.folders != undefined ) {
            for( idx in fileData.folders ) {
                root.add(new qx.ui.tree.TreeFolder(fileData.folders[idx]));
            }
        }
        if( typeof fileData.files != undefined ) {
            for( idx in fileData.files ) {
                root.add(new qx.ui.tree.TreeFile(fileData.files[idx]));
            }
        }
    },

    setTree : function(fileData) {
        qx.dev.Debug.debugObject(fileData);
        this._setTree(this.__tree, fileData);
//        var root = this.__tree.getRoot();
//        if( typeof fileData.folders != undefined ) {
//            for( idx in fileData.folders ) {
//                root.add(new qx.ui.tree.TreeFolder(fileData.folders[idx]));
//            }
//        }
//        if( typeof fileData.files != undefined ) {
//            for( idx in fileData.files ) {
//                root.add(new qx.ui.tree.TreeFile(fileData.files[idx]));
//            }
//        }
    }
  }
});
